﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _180FrameWork.CustControls
{
    public partial class CustTreeView : TreeView
    {
        public CustTreeView()
        {
            InitializeComponent();
          
            this.imglist.Images.Add("root", CustIconHelper.GetIcon(CustIcon.root));
            this.imglist.Images.Add("folder", CustIconHelper.GetIcon(CustIcon.folder));
            this.imglist.Images.Add("item", CustIconHelper.GetIcon(CustIcon.item));
        }

        /// <summary>
        /// 树结构对象
        /// </summary>
        CustTreeViewItem root;

        /// <summary>
        /// 树结构对象
        /// </summary>
        /// <param name="root"></param>
        public void Create(CustTreeViewItem root)
        {
            this.Nodes.Clear();
            this.root = root;
            foreach (CustTreeViewItem chile in root.ChildList)
            {
                AddNode(this.Nodes, chile);
            }
        }

        /// <summary>
        /// 添加树节点
        /// </summary>
        /// <param name="parentitem"></param>
        /// <param name="m"></param>
        void AddNode(TreeNodeCollection parentitem, CustTreeViewItem m)
        {
            TreeNode item = new TreeNode(m.Caption);
            item.Tag = m;
            if (!m.Final)
            {
                item.ImageKey = "folder";
                item.SelectedImageKey = "folder";
            }
            else
            {
                item.ImageKey = "item";
                item.SelectedImageKey = "item";
            }
            item.Checked = m.Checked;
            parentitem.Add(item);
            foreach (CustTreeViewItem mchild in m.ChildList)
            {
                AddNode(item.Nodes, mchild);
            }
        }

        private bool _AutoLinkChild = false;
        /// <summary>
        /// 自动联动子项
        /// </summary>
        public bool AutoLinkChild { get { return this._AutoLinkChild; } set { this._AutoLinkChild = value; } }

        //private AutoLinkParentType _AutoLinkParent = AutoLinkParentType.None;
        ///// <summary>
        ///// 自动联动父项
        ///// </summary>
        //public AutoLinkParentType AutoLinkParent { get { return this._AutoLinkParent; } set { this._AutoLinkParent = value; } }

        private void CheckChildNode(TreeNode n)
        {
            foreach (TreeNode c in n.Nodes)
            {
                c.Checked = n.Checked;
                CheckChildNode(c);
            }
        }

        private void CheckParentNodeAbsolutely(TreeNode n)
        {
            var p = n.Parent;
            if (p != null)
            {
                p.Checked = n.Checked;
                CheckParentNodeAbsolutely(p);
            }
        }

        private void CheckParentNodeRelatively(TreeNode n)
        {
            var p = n.Parent;
            if (p != null)
            {
                //检查其他子项是否和当前项checkbox一致
                bool IsOtherChildFit=true;
                foreach (TreeNode c in p.Nodes)
                {
                    if (c.Checked != n.Checked)
                    {
                        IsOtherChildFit = false;
                        break;
                    }
                }
                if (IsOtherChildFit)
                {
                    p.Checked = n.Checked;
                    CheckParentNodeRelatively(p);
                }
            }
        }

        void RingTreeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (AutoLinkChild)
                CheckChildNode(e.Node);
        }
    }

}
